#coding:utf8
'''
Created on 2010-11-24

@author: root
'''

import htmlparser

import network
import datetime, socket, urllib2

import datetime, time
import urllib
import re 
import random

#about os version
from platform import system as os_type
#系统默认编码
sys_encoding='gbk' if os_type()=='Windows' else 'utf8'

from filesystem import *

#need install MYSQLdb on windows
# import database
# from database import dbgen, getdb

from pprint import pprint as dump


def join_host_path(host, path, protocal="http"):
	if path[0]!="/": host=host+"/"
	return protocal+"://"+host+path 

def get_nice_url(url, host=""):
	low_url=url.lower()
	if not low_url.startswith("http://"):
		#相对路径
		if url.startswith("#") \
			or low_url.startswith("mailto:") \
			or low_url.startswith("javascript:") \
			or url.find("://")!=-1 \
			or url.strip()=="": return None  #ignore ftp, https
		#print host, url
		url=join_host_path(host, url)
		#print url
	#绝对路径
	pos=url.find("#")
	if pos!=-1:
		url=url[:pos]
			
	low_url=url.lower()
	
	#排除后缀
	if low_url.endswith(".pdf") \
		or low_url.endswith(".jpg") or low_url.endswith(".jpeg") or low_url.endswith(".png") or low_url.endswith(".gif")\
		or low_url.endswith(".zip") or low_url.endswith(".rar") or low_url.endswith(".gzip") or low_url.endswith(".tar") or low_url.endswith(".jar")\
		or low_url.endswith(".gz") or low_url.endswith(".7z") or low_url.endswith(".exe") or low_url.endswith(".dll")\
		or low_url.endswith(".xml") or low_url.endswith(".js") or low_url.endswith(".css") \
		or low_url.endswith(".ppt") or low_url.endswith(".pptx") \
		or low_url.endswith(".xls") \
		or low_url.endswith(".doc") or low_url.endswith(".docx"): return None
	return url

def echo(s):
	for i in s:
		print i

def urlencode(url):
	gbk=unicode(url).encode('gbk')
	return repr(gbk).replace(r'\x','%')[1:-1].replace(' ', '%20')

#cjson tools
import cjson
def json_encode(obj):
	return cjson.encode(obj)
	
def json_decode(obj):
	return cjson.decode(obj)
	
def json_save(obj, fn):
	write_file(json_encode(obj), fn)
	
def json_load(fn, encoding=sys_encoding):
	return json_decode(read_file(fn).decode(encoding))
	
	
#time utils
global_time_seeds={}

'''
usage:
time_gap(1)
...code...
print time_gap(1)
print time_gap(1, False)
'''

def time_seed(seed):
	global global_time_seeds
	global_time_seeds[seed]=time.time()

def time_gap(seed, reset=True):
	global global_time_seeds
	now=time.time()
	if not seed in global_time_seeds:
		time_seed(seed)
		return 0
	else:
		last=global_time_seeds[seed]
		if reset:
			global_time_seeds[seed]=now
		return now-last
		
def time_gap2(seed1, seed2):
	global global_time_seeds
	return global_time_seeds[seed2]-global_time_seeds[seed1]
	
#other json
import simplejson, json


# def eval_file(fn):
	# return eval(read_file(fn))
	
# def include_file(name):
	# eval('from %s import *' % name)
	
	

def format_tab(s):
	ls=s.split('\n')
	ls=[l for l in ls if len(l.strip())>0]
	for i, l in enumerate(ls):
		l=l.strip()
		if len(l)<10: ls[i-1]+='\t'+l; ls[i]=''
	ls=[l for l in ls if len(l.strip())>0]
	print  '\n'.join(['\t'.join(l.split()) for l in ls])